package hotel.switchs.oversea.online.service.impl;

import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.switchs.oversea.online.service.TestRetryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Author lmf
 * @Date 2020/6/29 17:28
 **/
@Service
@Slf4j
public class TestRetryServiceImpl extends BaseServiceImpl implements TestRetryService {

    //异步重试
    @Async(value = "executor")
    public void asyncRetry() throws Exception {
        test();
    }
    //value 什么情况下唤起重试 maxAttempts重试次数 delay 重试间隔 multiplier是上次重试时间的倍数
    @Override
    @Retryable(value = Exception.class,maxAttempts = 4,backoff =@Backoff(delay = 2000,multiplier = 1.5))
    public String test() throws Exception {
       int i=0;
       log.info("====进来l====");
       if(i<3){
           i++;
           throw new Exception("数据不对");
       }
       return "jaj";
    }

    /**
     * 使用重试错误，回调返回类型必须跟Retryable注解的方法一样
     * @param e
     * @return
     */
    @Recover
    public String recover(Exception e){
        return "重试错误";
    }
}
